package com.handydata.antifraud.rule.funcs;

import com.handydata.antifraud.common.constant.AlarmInfoCode;
import com.handydata.antifraud.common.constant.DictInfoCode;
import com.handydata.antifraud.to.RiskInputTO;
import com.handydata.antifraud.to.RiskMiddleTO;
import com.handydata.common.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 计算手机号归属地禁止区域
 */
@Slf4j
@Component
public class CalcPhoneAttributionForbiddenArea extends CalculateAdapter {

    private static final String cityKey = AlarmInfoCode.REDIS_GENERAL_DICT.getCode() + "_" + DictInfoCode.DICT_FORBIDDEN_CITY.getCode() + "_";
    private static final String provinceKey = AlarmInfoCode.REDIS_GENERAL_DICT.getCode() + "_" + DictInfoCode.DICT_FORBIDDEN_PROVINCE.getCode() + "_";

    @Autowired
    private RedisUtil redisUtil;

    public void doCalc(RiskInputTO riskInputTO, RiskMiddleTO riskMiddleTO) {
        String phoneCity = riskInputTO.getPhoneCity();
        String province, city;
        if(StringUtils.isEmpty(phoneCity))
            return;
        if(phoneCity.contains(",")) {
            province = phoneCity.split(",")[0];
            city = phoneCity.split(",")[1];
        } else {
            province = phoneCity;
            city = phoneCity;
        }
        log.info(" start cac CalcPhoneAttributionForbiddenArea province{},city{}",province,city);
        String provinceHit = redisUtil.getString(provinceKey+province);
        String cityHit = redisUtil.getString(cityKey+city);

        int res = convert(provinceHit) & convert(cityHit);
        riskMiddleTO.setCalAHdyR006SJGSD(String.valueOf(res));
        log.info(" finish cac CalcPhoneAttributionForbiddenArea provinceHit{} cityHit{}",provinceHit,cityHit);

    }
    private static int convert(String str) {
        if(str==null||str.length()<=0)
            return 0;
        return 1;
    }

}
